package com.kpmg.datalake.common.utils;

import com.kpmg.datalake.db.model.Project;
import com.kpmg.datalake.db.model.SysUsr;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import org.apache.commons.codec.binary.Base64;

/**
 * 工具类
 * @author Tony
 */
public class Utils {
	
	private Utils() {
		
	}

	public static final String DES = "AES"; // optional value AES/DES/DESede

	public static final  String CIPHER_ALGORITHM = "AES"; // optional value AES/DES/DESede

	public static Key getSecretKey(String key) throws NoSuchAlgorithmException {
		SecretKey securekey = null;
		if (key == null) {
			key = "";
		}
		KeyGenerator keyGenerator = KeyGenerator.getInstance(DES);
		keyGenerator.init(new SecureRandom(key.getBytes()));
		securekey = keyGenerator.generateKey();
		return securekey;
	}

	public static String encrypt(String data, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
		SecureRandom sr = new SecureRandom();
		Key securekey = getSecretKey(key);
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
		byte[] bt = cipher.doFinal(data.getBytes());
		return Base64.encodeBase64String(bt);
	}

	public static String detrypt(String message, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		SecureRandom sr = new SecureRandom();
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		Key securekey = getSecretKey(key);
		cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
		byte[] res = Base64.decodeBase64(message);
		res = cipher.doFinal(res);
		return new String(res);
	}

	/**
	 * 月份格式化 如1->01
	 * 
	 * @param month
	 * @return
	 */
	public static String formatMonth(String month) {
		String monthValue = month;
		if (month.length() < 2) {
			monthValue = "0" + month;
		}
		return monthValue;
	}

	/**
	 * 获取请求号
	 * 
	 * @return
	 */
	public static String getRequestNo() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return format.format(new Date());
	}

	/**
	 * 获取GUID
	 * 
	 * @return
	 */
	public static String getGUID() {
		return UUID.randomUUID().toString().replace("-", "").toUpperCase();
	}

	/**
	 * 格式化日期显示
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateTime(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}

	/**
	 * 依据用户信息获取项目信息
	 * 
	 * @param projectId
	 * @param user
	 * @return
	 */
	public static Project getProject(String projectId, SysUsr user) {
		Project project = null;
		if (user.getProjects() != null) {
			for (Project p : user.getProjects()) {
				if (p.getPrjId().equals(projectId)) {
					project = p;
				}
			}
		}
		return project;
	}

	/**
	 * 获取请求编号
	 * 
	 * @param requestCount
	 * @return
	 */
	public static String formatRequestCd(int requestCount) {
		StringBuilder requestCd = new StringBuilder();
		requestCount = requestCount == 0 ? 1 : requestCount;
		requestCd.append(requestCount);
		int length = 3;
		for (int i = 0; i < (length - String.valueOf(requestCount).length()); i++) {
			requestCd.insert(0, "0");
		}
		return requestCd.toString();
	}

}
